package com.mh.zookeeper.zkclient;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import static com.mh.zookeeper.PropertiesUtils.getZookeeperConnectStr;

public class NodeAPI {


    public static void main(String[] args) throws InterruptedException {

        ZkClient zkClient = new ZkClient(getZookeeperConnectStr());
        System.out.println("会话被创建了..");

        String path = "/lg-zkClient-Ep";
        boolean exists = zkClient.exists(path);

        if(!exists){
            zkClient.createEphemeral(path,"123");
        }

        Object o = zkClient.readData(path);
        System.out.println(o);


        zkClient.subscribeDataChanges(path, new IZkDataListener() {

            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println(s+"该节点内容被更新，更新的内容"+o);
            }


            public void handleDataDeleted(String s) throws Exception {
                System.out.println(s+"该节点被删除");
            }
        });


        zkClient.writeData(path,"456");
        Thread.sleep(1000);

        zkClient.delete(path);
        Thread.sleep(1000);

    }

}
